# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution    
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields
from osv import osv
import ir

import netsvc
import time
from mx import DateTime

logger = netsvc.Logger()

class mrp_production(osv.osv):
    
    _inherit = 'mrp.production'
    _name = 'mrp.production'
    
    _columns = {
        'expiry_handled': fields.boolean('is Expiry Handling Done', required=False),
    }
    _defaults = {
        'expiry_handled': lambda *a:False,
    }
    
    def assign_lot_number_fn(self, cr, uid, ids):
        
        stock_move_obj = self.pool.get('stock.move')
        for production in self.browse(cr, uid, ids):
            productsLotsaMap ={}
            productLotQTYMap ={}
            
            for pickingMoves in production.picking_id.move_lines:
                 if productsLotsaMap.has_key(pickingMoves.product_id.id):
                     productLotQTYMap = productsLotsaMap[pickingMoves.product_id.id]
                 else:
                     productLotQTYMap ={}
                 productLotQTYMap[pickingMoves.prodlot_id.id] = [pickingMoves, pickingMoves.product_qty]
                 productsLotsaMap[pickingMoves.product_id.id] = productLotQTYMap

            logger.notifyChannel('assign_lot_number_fn', netsvc.LOG_WARNING, 'productsLotsaMap array:%s' %(productsLotsaMap))
            consumedMovesSQL = (''' select move_id from mrp_production_move_ids where production_id = %s''' % (production.id))
            cr.execute(consumedMovesSQL)
            
            moves= [] 
            for line in stock_move_obj.browse(cr, uid, [y for y, in cr.fetchall()]):			
                if productsLotsaMap.has_key(line.product_id.id):
                    productLotQTYMap = productsLotsaMap[line.product_id.id]
                    firstMoveLine =True
                    for toUseProdLot , moveQTYArray in productLotQTYMap.items():
                        relatedMoveInPicking= moveQTYArray[0]
                        lotQty= moveQTYArray[1]
                        if firstMoveLine:
                            firstMoveLine =False
                            stock_move_obj.write(cr, uid, [line.id] , {'prodlot_id': toUseProdLot, 'product_qty':lotQty})
                            #stock_move_obj.write(cr, uid, [relatedMoveInPicking] , {'move_dest_id':[(6,0,line.id)]})
                            relatedMoveInPicking.move_dest_id = line.id
                            moves.append(line.id)
                        else:
                            #use create instead of copy, as copy create extra moves in picking
                            #moveID = stock_move_obj.copy(cr, uid, line.id , {'prodlot_id': toUseProdLot, 'product_qty':lotQty, 'state':'assigned'})
                            moveID = self.pool.get('stock.move').create(cr, uid, {
                            'name':'PROD:'+line.name,
                            'date_planned': line.date_planned,
                            'product_id': line.product_id.id,
                            'product_uom': line.product_uom.id,
                            'product_uos_qty': line.product_uos and line.product_uos_qty or False,
                            'product_uos': line.product_uos and line.product_uos.id or False,
                            'location_id': line.location_id.id,
                            'location_dest_id': line.location_dest_id.id,
                            'move_dest_id': line.move_dest_id.id,
                            'prodlot_id': toUseProdLot,
                            'product_qty':lotQty,
                            'state': 'assigned',
                        })
                            relatedMoveInPicking.move_dest_id = moveID
                            moves.append(moveID)
                            
            self.write(cr, uid, [production.id], {'move_lines': [(6,0,moves)]})
      
		
		
    
mrp_production()
